﻿using System;
using System.Collections.Generic;
using System.Text;
using SIOsystem.Model.OrderDataSetTableAdapters;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.ComponentModel;
using SIOsystem.Model.ReportDataSetTableAdapters;

namespace SIOsystem.Control
{
    class OrderClass
    {
        static public bool insertOrder(BindingList<OrderListClass> orderList,string InputName,long OID)
        {
            try
            {
                if (orderList.Count>0)
                {
                    using (InsertInOrderTableAdapter db = new InsertInOrderTableAdapter())
                    {
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        long autoID = db.InsertInOrder(InputName, OID);
                        if (autoID > 0)
                        {
                            OleDbCommand cmd = conn.CreateCommand();
                            cmd.Transaction = tr;
                            cmd.CommandText = @"select @@identity";
                            autoID = long.Parse(cmd.ExecuteScalar().ToString());
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                        int serialsCount = 0;
                        foreach (OrderListClass ol in orderList)
                        {
                            serialsCount += db.InsertInSerials(ol.Mid, ol.Pid, ol.Tid, ol.ProductName, ol.Amt, ol.Price, autoID);
                        }
                        if (serialsCount==orderList.Count)
                        {
                            tr.Commit();
                            return true;
                        } 
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public bool insertOrder(BindingList<OrderListClass> orderList, string OutputName, long DID, long OID)
        {
            try
            {
                if (orderList.Count > 0)
                {
                    using (InsertOutOrderTableAdapter db = new InsertOutOrderTableAdapter())
                    {
                        OleDbConnection conn = db.Connection;
                        conn.Open();
                        OleDbTransaction tr = conn.BeginTransaction();
                        db.Transaction = tr;
                        long autoID = db.InsertOutOrder(OutputName, OID, DID);
                        if (autoID > 0)
                        {
                            OleDbCommand cmd = conn.CreateCommand();
                            cmd.Transaction = tr;
                            cmd.CommandText = @"select @@identity";
                            autoID = long.Parse(cmd.ExecuteScalar().ToString());
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                        int serialsCount = 0;
                        foreach (OrderListClass ol in orderList)
                        {
                            serialsCount += db.InsertOutSerials(ol.ISID, ol.Amt,  autoID);
                        }
                        if (serialsCount == orderList.Count)
                        {
                            tr.Commit();
                            return true;
                        }
                        else
                        {
                            tr.Rollback();
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public DataTable getStorageList(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    return db.GetSPListById(tid);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public double[] getStorage(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    DataTable dt = db.GetSPById(tid, tid);
                    if (dt.Rows.Count == 1)
                    {
                        return new double[] { Convert.ToDouble(dt.Rows[0]["库存"]), Convert.ToDouble(dt.Rows[0]["价格"]) };
                    }
                    else
                    {
                        return new double[] { 0, 0 };
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public string[] getProductInfo(long tid)
        {
            try
            {
                using (StorageTableAdapter db = new StorageTableAdapter())
                {
                    DataTable dt = db.GetSPById(tid, tid);
                    if (dt.Rows.Count == 1)
                    {
                        return new string[] { dt.Rows[0]["库存"].ToString(), dt.Rows[0]["价格"].ToString(), dt.Rows[0]["单位"].ToString() };
                    }
                    else
                    {
                        return new string[] { "", "", "" };
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        static public int updateNewPrice(long isid, double newPrice)
        {
            try
            {
                using (CommandTableAdapter db = new CommandTableAdapter())
                {
                    return db.UpdateInPrice(newPrice, isid);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

    }
}
